/**
 * Definition for singly-linked list. */
function ListNode(val, next) {
    this.val = (val===undefined ? 0 : val)
    this.next = (next===undefined ? null : next)
}

/*
class ListNode {
  constructor(val) {
    this.val = val;
    this.next = null;
  }
}
* */


// 字符串转一维整型数组
/**
 * @param {string} input
 * @return {Array}
 */
const stringToIntegerArray = function (input) {
    console.log("stringToIntegerArray input: ", input);
    // input.trim is not a function
    // TypeError: input.trim is not a function 加这个
    if (typeof input !== 'string') {
        return [];
    }

    input = input.trim();
    input = input.substring(1, input.length-1);
    if (input.length === 0) {
        return [];
    }

    const parts = input.split(",");
    const output = new Array(parts.length).fill(0);
    for (let index=0; index<parts.length; ++index) {
        let part = parts[index].trim();
        output[index] = parseInt(part);
    }
    return output;
}

// 字符串转链表
/**
 * @param {string} input
 * @return {ListNode}
 */
const stringToListNode = function (input) {
    // const nodeValues = stringToIntegerArray(input);
    const nodeValues = JSON.parse(input);
    console.log("nodeValues: ", nodeValues);
    // 这样像new Array可以吧？
    let dummyRoot = new ListNode(0);
    let ptr = dummyRoot;
    // let item:nodeValues 遍历报错，ts不支持？！！
    for (let i=0; i<nodeValues.length; ++i) {
        let item = nodeValues[i];
        // 这样像new Array可以吧？
        ptr.next = new ListNode(item);
        ptr = ptr.next;
    }
    return dummyRoot.next;
}


// root:  undefined ??? 为啥？？
/**
 * @param {string} input
 * @return {ListNode}
 */
const stringToLinkedList = function (input) {
    input = input.trim();
    input = input.substring(1, input.length - 1);
    if (input.length === 0) {
        return null;
    }

    const parts = input.split(",");
    const nodes = parts.map((part) => new ListNode(parseInt(part)));
    for (let i = 0; i < nodes.length - 1; i++) {
        nodes[i].next = nodes[i + 1];
    }
    return nodes[0];
};

// 链表转字符串
/**
 * @param {ListNode} node
 * @return {string}
 */
const listNodeToString = function (node) {
    console.log("node: ", node);
    if (node === null) {
        return "[]";
    }
    let result = "";
    while (node != null) {
        // ", " 空格必须的，要不要改下面的substring 0~2
        result += node.val + ", ";
        node = node.next;
    }
    console.log("result: ", result);

    return "[" + result.substring(0, result.length - 2) + "]";
}

/**
 * @param {ListNode} node
 * @return {string}
 */
const prettyPrintLinkedList = function (node) {
    let ans = "";
    while (node != null && node.next != null) {
        // 不能用换行
        // console.log(node.val + "->");
        ans += node.val + "->";
        node = node.next;
    }

    if (node != null) {
        // console.log(node.val);
        ans += node.val;
    } else {
        console.log("Empty LinkedList!");
    }
    console.log("ans: ", ans);
}

// module.exports = prettyPrintLinkedList;
// module.exports = listNodeToString;
module.exports = stringToListNode;
// module.exports = stringToLinkedList;
// module.exports = stringToIntegerArray;
// module.exports = ListNode;